<!--签约管理-->
<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="car-label">关键字:</div>
      <el-input v-model="listQuery.keyString" placeholder="姓名/手机号/身份证号" style="width: 200px;" class="filter-item" />
      <div class="car-label">贷款银行:</div>
      <el-select v-model="listQuery.bank" placeholder="请选择" clearable class="filter-item" style="width: 130px">
        <el-option v-for="item in banks" :key="item.id" :label="item.bankName" :value="item.id"  />
      </el-select>
      <div class="car-label">垫款时间:</div>
      <el-date-picker v-model="listQuery.moneyAdvancedDate" placeholder="请选择垫款时间" clearable style="width: 130px" class="filter-item" />
      <div class="car-label">签约状态:</div>
      <el-select v-model="listQuery.signStatus" style="width: 140px" class="filter-item">
        <el-option v-for="item in signStatuses" :key="item.key" :label="item.label" :value="item.key" />
      </el-select>
      <el-button v-waves style="margin-left: 10px" class="filter-item" type="primary" icon="el-icon-search" @click="search">
        搜索
      </el-button>
      <el-button v-waves :loading="downloadLoading" class="filter-item right-btn" type="danger"  @click="toReceive">
        收件
      </el-button>
      <el-button v-waves :loading="downloadLoading" class="filter-item right-btn" type="primary"  @click="toSend">
        寄送
      </el-button>
    </div>
    <ve-table
      style="width:100%"
      :columns="columns"
      :table-data="tableData"
      :border-x="true"
      row-key-field-name="id"
      :border-y="true"
      :row-style-option="rowStyleOption"
      :checkbox-option="checkboxOptipon"
    />
    <div class="table-pagination">
      <ve-pagination
        :total="totalCount"
        :page-index="pageIndex"
        :page-size="pageSize"
        @on-page-number-change="pageNumberChange"
        @on-page-size-change="pageSizeChange"
      />
    </div>
  </div>
</template>

<style lang="scss" scoped>
.table-pagination {
  margin-top: 20px;
  text-align: right;
}
.right-btn{
  float: right;
}
</style>

<script>

export default {
  data() {
    return {
      // page index
      pageIndex: 1,
      // page size
      pageSize: 10,
      tableData: [],
      banks:[],
      totalCount: '',
      listQuery: {},
      signStatuses:[{key:0,label:"通过"},{key:1,label:"不通过"}],
      rowStyleOption: { // 表格样式
        stripe: true, // 斑马纹
        hoverHighlight: true, // hover高亮
        clickHighlight: true // 点击行高亮
      },
      selectedRow:null,
      signType: [],
      checkboxOptipon: {
        // 行选择改变事件
        selectedRowChange: ({ row, isSelected, selectedRowKeys }) => {
          this.selectedRow = isSelected ? row : null;
          selectedRowKeys = isSelected ? row.rowKey : null;
          console.log(row, isSelected, selectedRowKeys)
        },
        // 全选改变事件
        selectedAllChange: ({ isSelected, selectedRowKeys }) => {
          console.log(isSelected, selectedRowKeys)
        }
      },
      columns: [
        {
          field: '',
          key: '1',
          // type=checkbox
          type: 'checkbox',
          title: '',
          width: 50,
          align: 'center'
        },
        {
          field: '',
          key: 'a',
          title: '序号',
          align: 'center',
          renderBodyCell: ({ row, column, rowIndex }, h) => {
            return (this.pageIndex - 1) * this.pageSize + rowIndex + 1
          }
        },
        { field: 'orderId', key: 'c', title: '订单编号', align: 'center', width: '150px',
          renderBodyCell: ({ row, column, rowIndex }, h) => {
            console.log(row)
            return (
              <span class={'id-column'} on-click={() => this.gotoDetail(row.id)}>{row.orderId}</span>)
          } },
        { field: 'customerName', key: 'c', title: '客户姓名', align: 'left', width: '100px' },
        { field: 'loanAmount', key: 'd', title: '贷款金额', width: '' },
        { field: 'serviceGroup', key: 'e', title: '部门', width: '' },
        { field: 'serviceUser', key: 'f', title: '业务员', width: '' },
        { field: 'loanBank', key: 'g', title: '贷款银行', width: '' },
        { field: 'moneyAdvancedDate', key: 'h', title: '垫款时间', align: 'left', width: '100px' },
        { field: 'status', key: 'i', title: '签约状态', align: 'left', width: '100px' },
        { field: 'importType', key: 'j', title: '签约方式', align: 'left', width: '100px' }
      ]
    }
  },
  computed: {
  },
  created() {
    this.appId = sessionStorage.getItem("appId");
    this.initDatabase();
    this.getBankList();
    this.getSignTypeAll();
  },
  methods: {

    /**
     *收件操作
     */
    async toReceive(){
       let msg = "";
       if(this.selectedRow){
          if(this.selectedRow.status === '风控审批完成确认'){
            let param = {orderId:this.selectedRow.id,code:"order_sign_image_receive"}
            this.$prompt("请输入备注信息", "备注", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              inputValue: param.remark,
              inputPattern: /\S/
            }).then(res => {param.remark = res.value})
            const data = await this.$http.get('/flow/signManagement/save',param);
            if (data.code === 200) {
              msg = "所选订单已收件"
              this.initDatabase();
            }else{
              msg = "所选订单无法完成收件，服务器错误"
            }
          }else{
            msg = "所选订单状态非 风控审批完成确认，无法进行收件"
          }
       }else{
         msg = "请选择需要操作的订单"
       }
       if(msg != ""){
         this.$message({
           type: 'info',
           message: msg
         })
       }
      return
    },
    /**
     *寄送操作
     */
    async toSend(){
      let msg = "";
      if(this.selectedRow){
        if(this.selectedRow.status === '待寄送'){
          let param = {orderId:this.selectedRow.id,code:"order_sign_image_send"}
          this.$prompt("请输入备注信息", "备注", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            inputValue: param.remark,
            inputPattern: /\S/
          }).then(res => {param.remark = res.value})
          const data = await this.$http.post('/flow/signManagement/save',param);
          if (data.code === 200) {
            msg = "所选订单已寄送"
            this.initDatabase();
          }else{
            msg = "所选订单无法完成寄送，服务器错误"
          }
        }else{
          msg = "所选订单状态非 待寄送状态，无法进行操作"
        }
      }else{
        msg = "请选择需要操作的订单"
      }
      if(msg != ""){
        this.$message({
          type: 'info',
          message: msg
        })
      }
      return
    },
    /**
     * 跳转详情
     * @date 2021/7/5
     * @param id
     */
    gotoDetail(id) {
      this.$router.push({ path: '/a-content/manager/sign-manager/sign-detail', query: { id: id, source: 'signManagerList' }})
    },

    search(){
      this.pageIndex = 1;
      this.initDatabase();
    },
    // page number change
    pageNumberChange(pageIndex) {
      this.pageIndex = pageIndex;
      this.getBankList();
    },

    // page size change
    pageSizeChange(pageSize) {
      this.pageIndex = 1;
      this.pageSize = pageSize;
      this.getBankList();
    },
    /**
     * 获取银行列表数据
     * @date 2021-05-18
     */
    async getBankList () {
      let param = {appId: await sessionStorage.getItem("appId")};
      let data = await this.$http.post('/auth/bankSetup/listAll', param);
      if (data.code === 200) {
        this.banks = data.data;
      }
    },
    /**
     * 获取签约类型
     * @returns []
     */
    async getSignTypeAll(){
      const param = { appId: await sessionStorage.getItem('appId') }
      const data = await this.$http.post('/auth/signingMethod/list/' + this.pageIndex + '/' + this.pageSize, param)
      if (data.code === 200) {
        this.signType = data.data
      }
    },
    // Simulation table data
    async initDatabase() {
      this.tableData = [];
      this.listQuery.appid = this.appId;

      let data = await this.$http.post('/flow/signManagement/list/'+ this.pageIndex + "/" + this.pageSize, this.listQuery)
      // console.log(data);
      if (data.code === 200) {
        this.tableData = data.data.records;
        this.totalCount = data.data.total;
        console.log("签约：",this.tableData);
      } else {
        console.log("签约数据获取异常！")
      }
      console.log(this.tableData)
    }
  }
}
</script>
